package com.otod.base.support.spring;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonFailure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class RestResponseExceptionHandler extends ResponseEntityExceptionHandler {
	protected static final Logger logger = LoggerFactory.getLogger(RestResponseExceptionHandler.class);

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public String handleException(Exception ex) {
		if(ex instanceof AppException){
			return JsonFailure.factory(ex.getMessage());
		}
		logger.error("系统错误",ex);
        ex.printStackTrace();
        return JsonFailure.factory("系统错误");
	}

}
